﻿using UnityEditor;
using UnityEngine;

public static class BindGroundTile
{
    // private static byte[] grass_nums = new byte[]
    // {
    //     11, 31, 22, 2, 10, 30, 27, 18, 26, 219,
    //     107, 255, 214, 66, 106, 254, 251, 210, 250, 126,
    //     104, 248, 208, 64, 75, 223, 127, 86, 95, 91, 94,
    //     8, 24, 16, 0, 72, 216, 120, 80, 88, 122, 218,
    //     74, 222, 123, 82, 90,
    // };

    [MenuItem("Assets/Mini Tools/Conf/绑定Ground Tile", false, 301)]
    private static void Main()
    {
        var objects = Selection.objects;

        if (objects is not { Length: > 0 }) return;

        foreach (var item in objects)
        {
            if (item is not ScriptableObject obj) continue;

            var path = AssetDatabase.GetAssetPath(item);

            ProcessPerGameObject(obj, path);
        }

        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }

    private static void ProcessPerGameObject(ScriptableObject go, string path)
    {
        //if (!(go is GroundTileConf conf) || conf.main == null)
        //{
        //    return;
        //}
        //Debug.Log(go.name);

        //var sprite_path = AssetDatabase.GetAssetPath(conf.main);
        //Debug.Log(sprite_path);

        //var sprites = AssetDatabase.LoadAllAssetRepresentationsAtPath(sprite_path);

        //conf.items = new GroundTileConf.Item[grass_nums.Length];
        //conf.sprites = new Sprite[sprites.Length - grass_nums.Length];
        //for (int i = 0; i < sprites.Length; i++)
        //{
        //    Debug.Log(sprites[i].name);
        //    if (i < grass_nums.Length)
        //    {
        //        conf.items[i] = new GroundTileConf.Item()
        //        {
        //            type = grass_nums[i],
        //            Sprite = sprites[i] as Sprite
        //        };
        //    }
        //    else
        //    {
        //        conf.sprites[i - grass_nums.Length] = sprites[i] as Sprite;
        //    }
        //}
    }
}
